package com.jiadakeji.ppm.modules.weixin.entity;


import com.fasterxml.jackson.annotation.JsonProperty;
import com.jiadakeji.ppm.common.utils.*;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * 调用微信统一下单接口
 * 返回给细小程序的对象
 * @Author xuelongjiang
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "xml")//解析xml的根元素
public class WechatUnifiedorderRetrun {




    private String appId; // 小程序ID

    private String timeStamp;//时间戳

    private String nonceStr; // 随机串

    @JsonProperty("package")
    private String package1;//  数据包 微信本字段使用的是 package，所以在进行签名时需要替换package1 为package

    private String signType;// 签名方式

    @JsonProperty("paySign")
    private String sign ; //签名


    public WechatUnifiedorderRetrun() {
    }

    public WechatUnifiedorderRetrun(WechatUnifiedorderResponse response) {

        this.appId = response.getSub_appid();
        this.nonceStr = IdGen.uuid();
        this.signType = "MD5";
        this.package1 = "prepay_id="+response.getPrepay_id();
        this.timeStamp = String.valueOf(System.currentTimeMillis());//时间戳

    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(String timeStamp) {
        this.timeStamp = timeStamp;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public String getPackage1() {
        return package1;
    }

    public void setPackage1(String package1) {
        this.package1 = package1;
    }

    public String getSignType() {
        return signType;
    }

    public void setSignType(String signType) {
        this.signType = signType;
    }


    /**
     *  签名
     * @param wechatUnifiedorderRetrun
     * @return
     */
    public WechatUnifiedorderRetrun  getReturnSign(WechatUnifiedorderRetrun wechatUnifiedorderRetrun){

        try {
            String temp = ObjectSortAscll.getSortMap(wechatUnifiedorderRetrun);
            temp = temp.replaceAll("package1","package");
            System.out.println("没有连接key:"+temp);
            temp = temp+"&key="+ WechatUtils.wechat_key;//使用配置文件
            System.out.println("连接key:"+temp);
            String sign = MD5Utils.parseStrToMd5U32(temp);
            System.out.println("签名sign:"+sign);

            wechatUnifiedorderRetrun.setSign(sign);

        }catch (Exception e){
          e.printStackTrace();

        }

        return  wechatUnifiedorderRetrun;

    }

   /* public static void main(String[] args) {

         WechatUnifiedorderResponse responses= new WechatUnifiedorderResponse();
         responses.setAppid("wx2421b1c4370ec43b");
         responses.setPrepay_id("wx201411101639507cbf6ffd8b0779950874");

        WechatUnifiedorderRetrun wechatUnifiedorderRetrun = new WechatUnifiedorderRetrun(responses);

        wechatUnifiedorderRetrun.getReturnSign(wechatUnifiedorderRetrun);
        //logger.info(XmlUtils.objectToXml(wechatUnifiedorderRetrun));
        System.out.println(XmlUtils.objectToXml(wechatUnifiedorderRetrun).replaceAll("package1","package"));

    }*/


}
